import instance from "@/utils/request.js";
import router from "@/router/router.js";
import { ElMessage } from "element-plus";

/*
    已废弃
*/

function error(response) {
    if(response.data.code === "ERROR") {
        ElMessage({
            message: "预订失败！",
            type: "error"
        });
    }
}

function error_customer(response) {
    if(response.data.code === "ERROR_CUSTOMER") {
        ElMessage({
            message: "请先完善个人信息！",
            type: "error"
        });
    }
}

function success() {
    ElMessage({
        message: "预订成功！",
        type: "success"
    });
}

function catch_error(error) {
    if(error.response.request.status === 401) {
        ElMessage({
            message: "您的连接已被注销！",
            type: "error"
        });
        user.logout();
        router.push("/user/index");
        return;
    }
    ElMessage({
        message: "预订失败！",
        type: "error"
    });
}
// 预订客房
export function book_1(id, type, start, end, name) {
    instance.request({
        url: 'user/index/book',
        method: 'POST',
        data: {
            user_id: id,
            room_type: type,
            stay_start_time: start,
            stay_end_time: end,
            stay_name: name.name_1
        },
        headers: { "User-Id": id }
    })
        .then(function(response) {
            error(response);
            error_customer(response);
            if(response.data.code === "SUCCESS") {
                success();
                router.push("/user/center/roommessage/roommanage");
            }
        })
            .catch(function(error){
                catch_error(error);
            });
}

export function book_2(id, type, start, end, name) {
    instance.request({
        url: 'user/index/book',
        method: 'POST',
        data: {
            user_id: id,
            room_type: type,
            stay_start_time: start,
            stay_end_time: end,
            stay_name: name.name_1
        },
        headers: { "User-Id": id }
    })
        .then(function(response) {
            error(response);
            error_customer(response);
            if(response.data.code === "SUCCESS") {
                success();

                instance.request({
                    url: 'user/index/book',
                    method: 'POST',
                    data: {
                        user_id: id,
                        room_type: type,
                        stay_start_time: start,
                        stay_end_time: end,
                        stay_name: name.name_2
                    },
                    headers: { "User-Id": id }
                })
                    .then(function(response) {
                        error(response);
                        error_customer(response);
                        if(response.data.code === "SUCCESS") {
                            success();
                            router.push("/user/center/roommessage/roommanage");
                        }
                    })
                    .catch(function(error){
                        catch_error(error);
                    });
            }
        })
        .catch(function(error){
            catch_error(error);
        });
}

export function book_3(id, type, start, end, name) {
    instance.request({
        url: 'user/index/book',
        method: 'POST',
        data: {
            user_id: id,
            room_type: type,
            stay_start_time: start,
            stay_end_time: end,
            stay_name: name.name_1
        },
        headers: { "User-Id": id }
    })
        .then(function(response) {
            error(response);
            error_customer(response);
            if(response.data.code === "SUCCESS") {
                success();

                instance.request({
                    url: 'user/index/book',
                    method: 'POST',
                    data: {
                        user_id: id,
                        room_type: type,
                        stay_start_time: start,
                        stay_end_time: end,
                        stay_name: name.name_2
                    },
                    headers: { "User-Id": id }
                })
                    .then(function(response) {
                        error(response);
                        error_customer(response);
                        if(response.data.code === "SUCCESS") {
                            success();

                            instance.request({
                                url: 'user/index/book',
                                method: 'POST',
                                data: {
                                    user_id: id,
                                    room_type: type,
                                    stay_start_time: start,
                                    stay_end_time: end,
                                    stay_name: name.name_3
                                },
                                headers: { "User-Id": id }
                            })
                                .then(function(response) {
                                    error(response);
                                    error_customer(response);
                                    if(response.data.code === "SUCCESS") {
                                        success();
                                        router.push("/user/center/roommessage/roommanage");
                                    }
                                })
                                .catch(function(error){
                                    catch_error(error);
                                });
                        }
                    })
                    .catch(function(error){
                        catch_error(error);
                    });
            }
        })
        .catch(function(error){
            catch_error(error);
        });
}

export function book_4(id, type, start, end, name) {
    instance.request({
        url: 'user/index/book',
        method: 'POST',
        data: {
            user_id: id,
            room_type: type,
            stay_start_time: start,
            stay_end_time: end,
            stay_name: name.name_1
        },
        headers: { "User-Id": id }
    })
        .then(function(response) {
            error(response);
            error_customer(response);
            if(response.data.code === "SUCCESS") {
                success();

                instance.request({
                    url: 'user/index/book',
                    method: 'POST',
                    data: {
                        user_id: id,
                        room_type: type,
                        stay_start_time: start,
                        stay_end_time: end,
                        stay_name: name.name_2
                    },
                    headers: { "User-Id": id }
                })
                    .then(function(response) {
                        error(response);
                        error_customer(response);
                        if(response.data.code === "SUCCESS") {
                            success();

                            instance.request({
                                url: 'user/index/book',
                                method: 'POST',
                                data: {
                                    user_id: id,
                                    room_type: type,
                                    stay_start_time: start,
                                    stay_end_time: end,
                                    stay_name: name.name_3
                                },
                                headers: { "User-Id": id }
                            })
                                .then(function(response) {
                                    error(response);
                                    error_customer(response);
                                    if(response.data.code === "SUCCESS") {
                                        success();

                                        instance.request({
                                            url: 'user/index/book',
                                            method: 'POST',
                                            data: {
                                                user_id: id,
                                                room_type: type,
                                                stay_start_time: start,
                                                stay_end_time: end,
                                                stay_name: name.name_4
                                            },
                                            headers: { "User-Id": id }
                                        })
                                            .then(function(response) {
                                                error(response);
                                                error_customer(response);
                                                if(response.data.code === "SUCCESS") {
                                                    success();
                                                    router.push("/user/center/roommessage/roommanage");
                                                }
                                            })
                                            .catch(function(error){
                                                catch_error(error);
                                            });
                                    }
                                })
                                .catch(function(error){
                                    catch_error(error);
                                });
                        }
                    })
                    .catch(function(error){
                        catch_error(error);
                    });
            }
        })
        .catch(function(error){
            catch_error(error);
        });
}

export function book_5(id, type, start, end, name) {
    instance.request({
        url: 'user/index/book',
        method: 'POST',
        data: {
            user_id: id,
            room_type: type,
            stay_start_time: start,
            stay_end_time: end,
            stay_name: name.name_1
        },
        headers: { "User-Id": id }
    })
        .then(function(response) {
            error(response);
            error_customer(response);
            if(response.data.code === "SUCCESS") {
                success();

                instance.request({
                    url: 'user/index/book',
                    method: 'POST',
                    data: {
                        user_id: id,
                        room_type: type,
                        stay_start_time: start,
                        stay_end_time: end,
                        stay_name: name.name_2
                    },
                    headers: { "User-Id": id }
                })
                    .then(function(response) {
                        error(response);
                        error_customer(response);
                        if(response.data.code === "SUCCESS") {
                            success();

                            instance.request({
                                url: 'user/index/book',
                                method: 'POST',
                                data: {
                                    user_id: id,
                                    room_type: type,
                                    stay_start_time: start,
                                    stay_end_time: end,
                                    stay_name: name.name_3
                                },
                                headers: { "User-Id": id }
                            })
                                .then(function(response) {
                                    error(response);
                                    error_customer(response);
                                    if(response.data.code === "SUCCESS") {
                                        success();

                                        instance.request({
                                            url: 'user/index/book',
                                            method: 'POST',
                                            data: {
                                                user_id: id,
                                                room_type: type,
                                                stay_start_time: start,
                                                stay_end_time: end,
                                                stay_name: name.name_4
                                            },
                                            headers: { "User-Id": id }
                                        })
                                            .then(function(response) {
                                                error(response);
                                                error_customer(response);
                                                if(response.data.code === "SUCCESS") {
                                                    success();

                                                    instance.request({
                                                        url: 'user/index/book',
                                                        method: 'POST',
                                                        data: {
                                                            user_id: id,
                                                            room_type: type,
                                                            stay_start_time: start,
                                                            stay_end_time: end,
                                                            stay_name: name.name_5
                                                        },
                                                        headers: { "User-Id": id }
                                                    })
                                                        .then(function(response) {
                                                            error(response);
                                                            error_customer(response);
                                                            if(response.data.code === "SUCCESS") {
                                                                success();
                                                                router.push("/user/center/roommessage/roommanage");
                                                            }
                                                        })
                                                        .catch(function(error){
                                                            catch_error(error);
                                                        });
                                                }
                                            })
                                            .catch(function(error){
                                                catch_error(error);
                                            });
                                    }
                                })
                                .catch(function(error){
                                    catch_error(error);
                                });
                        }
                    })
                    .catch(function(error){
                        catch_error(error);
                    });
            }
        })
        .catch(function(error){
            catch_error(error);
        });
}